from typing import List, Dict
from zhipuai import ZhipuAI

MODEL = "glm-4-plus"
client = ZhipuAI(api_key='652056ee12c005f36b61ba8df67274b2.US6dlqS8ckjtuQv3')

class StructuringAgent:

    def structure_review(self, summaries: List[Dict], comparison: str, review_type: str = 'status') -> str:
        """
        结构化智能体：组织和结构化综述内容
        :param summaries: 论文摘要列表，每个元素包含title、summary和key_findings
        :param comparison: 比较分析结果
        :param review_type: 综述类型
        :return: 返回结构化的综述内容
        """
        try:
            summaries_text = "\n\n".join([f"Paper: {s['title']}\nSummary: {s['summary']}\nKey Findings: {', '.join(s['key_findings'])}" for s in summaries])
            
            prompt = f"""As an academic research expert, please structure a {review_type} literature review based on the provided paper summaries and integrated analysis. The review should:
            1. Organizes content logically and coherently into distinct sections
            2. Highlights key themes and patterns across papers
            3. Integrates the comparative insights within each relevant section
            4. Maintains academic rigor while ensuring accessibility
            5. Synthesizes core insights and methodological approaches
            6. Discusses implications and future research directions
            7. Seamlessly incorporates comparative analysis without explicit section headers

            Paper Summaries:
            {summaries_text}

            Additional Analysis:
            {comparison}"""

            response = client.chat.completions.create(
                model=MODEL,
                messages=[{"role": "system", "content": "You are a research content organization expert who excels at synthesizing academic literature in a clear and accessible manner."}, 
                          {"role": "user", "content": prompt}],
                temperature=0.2
            )
            
            return response.choices[0].message.content
        except Exception as e:
            print(f"结构化内容生成失败: {str(e)}")
            return "结构化内容生成失败"